package com.sita.bluetactivity.Base;

import android.content.res.Resources;
import android.text.TextUtils;

import com.sita.bluetactivity.R;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by xiaodong on 16/2/17.
 */
public class DateUtils {
    public static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm");
    public static final SimpleDateFormat DATETIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static final SimpleDateFormat YYYYMMDD_TIME = new SimpleDateFormat("yyyy年MM月dd日");
    public static final SimpleDateFormat YYYYMM = new SimpleDateFormat("yyyy年MM月");
    public static final SimpleDateFormat HHMMSS = new SimpleDateFormat("HH:mm:ss");

    public static String translateDateTime(Long time) {
        long oneDay = 24 * 60 * 60 * 1000;
        Calendar current = Calendar.getInstance();
        Calendar today = Calendar.getInstance();    //今天

        today.set(Calendar.YEAR, current.get(Calendar.YEAR));
        today.set(Calendar.MONTH, current.get(Calendar.MONTH));
        today.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));
        //  Calendar.HOUR——12小时制的小时数 Calendar.HOUR_OF_DAY——24小时制的小时数
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);

        long todayStartTime = today.getTimeInMillis();

        String prefix = "";
        if (android.text.format.DateUtils.isToday(time)) { // today
            long d = System.currentTimeMillis() - time;
            if (d <= 60) {
                return (String) BaseApplication.getContext().getText(R.string.just_before);
            }
            prefix = (String)BaseApplication.getContext().getText(R.string.today);
        } else if (time >= todayStartTime - oneDay && time < todayStartTime) { // yesterday

            prefix = (String) BaseApplication.getContext().getText(R.string.day_before);
        } else {
            long days = (time - todayStartTime) / oneDay;
            prefix = Math.abs(days) + (String) BaseApplication.getContext().getText(R.string.day_before);
        }
        String dateStr = TIME_FORMAT.format(new Date(time));
        if (!TextUtils.isEmpty(dateStr) && dateStr.contains(" 0")) {
            dateStr = dateStr.replace(" 0", " ");
        }
        return prefix + " " + dateStr;
    }

    public static String formatDuration(long duration) {
        Resources res = BaseApplication.getContext().getResources();
        long timePeriod = duration / 1000;
        long hours = timePeriod / 3600;
        long minutes = (timePeriod % 3600) / 60;
        long seconds = timePeriod % 60;
        if (seconds > 0){
            minutes += 1;
        }
        return hours + res.getString(R.string.hours) +  minutes + res.getString(R.string.minutes);
    }

    public static boolean isSameDay(long time1, long time2){
        final Calendar firstCal = Calendar.getInstance();
        final Calendar secondCal = Calendar.getInstance();
        firstCal.setTimeInMillis(time1);
        secondCal.setTimeInMillis(time2);
        return (firstCal.get(Calendar.ERA) == secondCal.get(Calendar.ERA) &&
                firstCal.get(Calendar.YEAR) == secondCal.get(Calendar.YEAR) &&
                firstCal.get(Calendar.DAY_OF_YEAR) == secondCal.get(Calendar.DAY_OF_YEAR));
    }

    public static String formatDateTime(Date date){
        if (isSameDay(date.getTime(), System.currentTimeMillis())){
            return TIME_FORMAT.format(date);
        } else {
            return DATETIME_FORMAT.format(date);
        }
    }

    public static String formatDateTime(long time){
        if (isSameDay(time, System.currentTimeMillis())){
            return TIME_FORMAT.format(new Date(time));
        } else {
            return DATETIME_FORMAT.format(new Date(time));
        }
    }

    public static String formatDate(long time, SimpleDateFormat formatter){
        return formatter.format(new Date(time));
    }

    private static String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
    public static String formatDayOfWeek(long time){
        String sub = YYYYMMDD_TIME.format(new Date(time));
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(time);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if(week_index<0){
            week_index = 0;
        }
        return sub + " " + weeks[week_index];
    }
}
